package com.tianji.learning.handler;

import com.tianji.common.utils.CollUtils;
import com.tianji.common.utils.DateUtils;
import com.tianji.learning.constants.LearningConstants;
import com.tianji.learning.constants.RedisConstants;
import com.tianji.learning.domain.po.PointsBoard;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.tianji.learning.service.IPointsBoardService;
import com.tianji.learning.service.IPointsRecordService;
import com.tianji.learning.utils.TableInfoContext;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Component
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class PointsBoardPersistentHandler {

    private IPointsBoardSeasonService pointsBoardSeasonService;
    private IPointsBoardService pointsBoardService;
    private StringRedisTemplate redisTemplate;
    private IPointsRecordService recordService;

//    @Scheduled(cron = "0 0 3 1 * ?")
    @XxlJob("creatTableJob")
    public void createPointsBoard(){
        //1.获得上个赛季的时间
        LocalDate date = LocalDate.now().minusDays(15);
        //2.获得上个赛季的赛季id
        Integer season= pointsBoardSeasonService.queryBoardSeasonId(date);
        //2.在mql中创建赛季表
        pointsBoardService.creatHistoryPointsBoard(season);
    }

    @XxlJob("savePointsBoard2DB")
    public void savePointsBoard2DB(){
        //1.获得上月时间
        LocalDate date = LocalDate.now().minusDays(15);
        //2.动态获得表名
        //2.1查询赛季信息
        Integer season= pointsBoardSeasonService.queryBoardSeasonId(date);
        //2.2将动态获得的表名放入TreadLocal中
        TableInfoContext.setInfo(LearningConstants.POINTS_BOARD_TABLE_PREFIX+season);
        //3.根据表名查询redis中的信息
        //3.1拼接key
        String key= RedisConstants.POINTS_RECORD_KEY_PREFIX
                +date.format(DateUtils.BOARD_DATE_SUFFIX_FORMATTER);

        //多服务实例部署  index起始页从0开始  total服务器总个数
        /*int index = XxlJobHelper.getShardIndex();
        int total = XxlJobHelper.getShardTotal();
        Integer pageNo=index+1;*/
        Integer pageNo=1;
        Integer pageSize=1000;
        while (true){
            List<PointsBoard> boardList = pointsBoardService
                    .queryCurrentPointsBoardList(key, pageNo, pageSize);
            //3.2没有信息则直接返回
            if (CollUtils.isEmpty(boardList)){
                break;
            }
            //3.3有信息则分页查询得到数据分批次写入数据库
//            //3.4将排名信息写到id
//            boardList.forEach(b->{
//                b.setId(b.get)
//            });

            //4.持久化
            pointsBoardService.saveBatch(boardList);
            //5.翻页
            pageNo++;

            //多实例部署  下一页=pageNo+total
           /* pageNo+=total;*/

        }
        //结束任务移除动态表名
        TableInfoContext.remove();
    }

    @XxlJob("clearPointsBoardFromRedis")
    public void clearPointsBoardFromRedis(){
        //1.获得上月时间
        LocalDate date = LocalDate.now().minusDays(15);
        //2拼接key
        String key= RedisConstants.POINTS_RECORD_KEY_PREFIX+date
                .format(DateUtils.BOARD_DATE_SUFFIX_FORMATTER);
        //3.清理数据
        redisTemplate.unlink(key);
    }

    //学习积分记录，每个月底清零 服务类
    @XxlJob("clearPointsRecordFromDB")
    public void clearPointsRecordFromDB(){
        //获得上个月的时间
        LocalDate date = LocalDate.now().minusDays(15);
        //获得上个月的起始时间
        LocalDateTime beginTime = DateUtils.getMonthBeginTime(date);
        LocalDateTime endTime = DateUtils.getMonthEndTime(date);
        recordService.clearPointsRecordFromDB(beginTime,endTime);
    }
}
